package com.itheima.lucene;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuceneFirst {
    public static void main(String[] args) throws IOException {
        createIndex();
        searchIndex();

    }

    static void createIndex() throws IOException {
        Directory directory = FSDirectory.open(new File("E:\\temp\\index").toPath());


        //引用自己定义的分析器
        IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());

        IndexWriter indexWriter = new IndexWriter(directory,config);

        File dir = new File("E:\\temp\\searchsource");

        File[] files = dir.listFiles();

        for (File f:files) {
            String fileName = f.getName();

            String filePath = f.getPath();

            String fileContent = FileUtils.readFileToString(f, "utf-8");

            long fileSize = FileUtils.sizeOf(f);

            Field fieldName = new TextField("name",fileName,Field.Store.YES);
            Field fieldPath = new TextField("path",filePath,Field.Store.YES);
            Field fieldContent = new TextField("content",fileContent,Field.Store.YES);
            Field fieldSize = new TextField("size",fileSize+"",Field.Store.YES);

            Document document = new Document();

            document.add(fieldName);
            document.add(fieldPath);
            document.add(fieldContent);
            document.add(fieldSize);

            indexWriter.addDocument(document);

        }
        indexWriter.close();
    }

    static void searchIndex() throws IOException {

        Directory directory = FSDirectory.open(new File("E:\\temp\\index").toPath());

        IndexReader indexReader = DirectoryReader.open(directory);

        IndexSearcher indexSearcher = new IndexSearcher(indexReader);

        Query query = new TermQuery(new Term("content","spring"));

        TopDocs topDocs = indexSearcher.search(query,10);


        System.out.println("查询总记录数"+topDocs.totalHits);

        ScoreDoc[] scoreDocs = topDocs.scoreDocs;

        for (ScoreDoc doc:scoreDocs
             ) {

            int docId = doc.doc;

            Document document = indexSearcher.doc(docId);
            System.out.println(document.get("name"));
            System.out.println(document.get("path"));
            System.out.println(document.get("size"));
            System.out.println(document.get("content"));


            System.out.println("****************************************************\n");
            
        }




    }


}

